
<template>
  <div>
    <!-- 搜索 :collapse="false" -->
    <TopFilter
      :list="topFilterInit"
      :cols="3"
      labelWidth="120"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>

    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :isSelectColumn="false"
      :params="fetchParams"
      :datakey="'records'"
      :onColumnsChange="columns => this.columns = columns"
      columnsRef="projectOperating_definition_majorClass_index_01"
      
    >
      <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新建</el-button>
      </template>
    </FilterTable>
    <!-- 编辑页面 -->
    <Drawer
      :visible.sync="visibleDrawer"
      :title="dialogTitle"
      :width="920"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <info-modal :type="handleType" :rowData="formData" @close="drawerClose" />
    </Drawer>
  </div>
</template>

<script>
import { marketTypeList } from '@/api/cms-api/projectOperating/definition/majorClass/index';
import { dictionary } from '@/utils/dictMixin';
import { formatdateByDay } from '@/utils/index.js';
import infoModal from './info';
export default {
  mixins: [dictionary],
  components: {
    infoModal
  },
  //初始化数据  全局变量
  data() {
    this.BaseTable = null;
    return {
      false: false,
      columns: this.creatHeader(),
      fetchApi: marketTypeList, //ipa 接口
      topFilterInit: this.topFilterInitFun(),
      fetchParams: { isparent: '1', xhrAbort: false },
      visibleDrawer: false,
      dialogTitle: '',
      handleType: '',
      formData: {}
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  //方法、事件
  methods: {
    formatDates: function(value) {
      // 这里的 value 就是需要过滤的数据
      if (value == null || value == '') {
        return '';
      }
      var date = new Date(value);
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      return year + '-' + month + '-' + day;
    },
    //搜索数据
    topFilterInitFun() {
      return [
        { type: 'INPUT', label: '活动大类代码', fieldName: 'typeCode', placeholder: '请填写', initialValue: '', focus: () => {} },
        { type: 'INPUT', label: '活动大类名称', fieldName: 'typeName', placeholder: '请填写', initialValue: '', focus: () => {} },
        { type: 'RANGE_DATE', label: '创建日期', fieldName: 'createdAt', valueFormat: 'yyyy-MM-dd', initialValue: '', placeholder: '请选择' },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'status',
          placeholder: '请选择',
          itemList: this.createDictList('1003'),
          focus: () => {}
        },
        {
          type: 'SELECT',
          label: '是否是广宣类活动',
          fieldName: 'isAdvertising',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          focus: () => {}
        },
        {
          type: 'SELECT',
          label: '是否需要二级审核',
          fieldName: 'isSecondaryAudit',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          focus: () => {}
        }
      ];
    },
    addHandle() {
      this.handleType = 'add';
      this.dialogTitle = `添加营销项目大类`;
      this.showHandler();
    },
    editHandle(row) {
      console.log(row);
      this.handleType = 'edit';
      this.dialogTitle = `编辑营销项目大类`;
      this.formData = row;
      this.showHandler();
    },
    showHandler() {
      this.initData = {
        type: 'add', //add/edit/show
        id: 1001
      };
      this.visibleDrawer = true;
    },
    //搜索获取数据
    changeHandle(val) {
      console.log(val);
      console.log(val.createdAt);
      var time = val.createdAt;
      val = Object.assign({}, { timestart: time[0] }, val);
      val = Object.assign({}, { timeend: time[1] }, val);
      val = Object.assign({}, { isparent: '1' }, val);
      console.log(val);
      this.fetchParams = Object.assign({}, val, { xhrAbort: false });
      //this.fetchParams = val;
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    //关闭详情
    drawerClose(val, type) {
      this.visibleDrawer = false;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },

    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  编辑
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '状态',
          dataIndex: 'status',
          sorter: true,
          filterItems: this.createDictList('1003'),
          render: props => {
            return <span>{this.createDictText(props.row.status, '1003')}</span>;
          }
        },
        { title: '活动大类代码', dataIndex: 'typeCode', sorter: true },
        { title: '活动大类名称', dataIndex: 'typeName', sorter: true },
        {
          title: '计划是否需要二级审核',
          dataIndex: 'isSecondaryAudit',
          sorter: true,
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.isSecondaryAudit, '1004')}</span>;
          }
        },
        {
          title: '是否广宣类活动',
          dataIndex: 'isAdvertising',
          sorter: true,
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.isAdvertising, '1004')}</span>;
          }
        },
        { title: '创建人', dataIndex: 'createdByName', sorter: true },
        {
          title: '创建时间',
          dataIndex: 'createdAt',
          sorter: true,
          render: props => {
            return <span>{formatdateByDay(props.row.createdAt)} </span>;
          }
        },
        { title: '修改人', dataIndex: 'updatedByName', sorter: true },
        {
          title: '修改时间',
          dataIndex: 'updatedAt',
          sorter: true,
          render: props => {
            return <span>{formatdateByDay(props.row.updatedAt)} </span>;
          }
        }
      ];
    }
  }
};
</script>

<style scoped>
</style>
